import typeOfUtils from './type-of-utils'
import htmlElementUtils from './htmlElementUtils'

class privateTextUtils {
  static visualDom: HTMLElement

  /**
   * 生成文本元素
   * @param value
   * @param size
   * @param family
   */
  static getVisualDom(value, size = 'inherit', family = 'inherit') {
    if (typeOfUtils.isEmpty(privateTextUtils.visualDom) || typeOfUtils.isNotHTMLDocument(privateTextUtils.visualDom)) {
      privateTextUtils.visualDom = document.createElement('span')
      htmlElementUtils.addChild(document.body, privateTextUtils.visualDom)
      privateTextUtils.visualDom.style.position = 'absolute'
      privateTextUtils.visualDom.style.visibility = 'hidden'
      privateTextUtils.visualDom.style.whiteSpace = 'nowrap'
      privateTextUtils.visualDom.style.zIndex = String(-100)
    }
    privateTextUtils.visualDom.style.fontSize = size
    privateTextUtils.visualDom.style.fontFamily = family
    privateTextUtils.visualDom.innerHTML = value
    return privateTextUtils.visualDom
  }
}

class textUtils {
  /**
   * 文本长度计算
   * @param value
   * @param size
   * @param family
   * @return {number}
   */
  static visualLength(value, size = 'inherit', family = 'inherit') {
    const visualDom = privateTextUtils.getVisualDom(value, size, family)
    return visualDom.offsetWidth
  }

  /**
   * 字体高度
   * @param value
   * @param size
   * @param family
   * @return {number}
   */
  static visualHeight(value, size = 'inherit', family = 'inherit') {
    const visualDom = privateTextUtils.getVisualDom(value, size, family)
    return visualDom.offsetHeight
  }
}

export default textUtils
